# coding: utf-8

import numpy as np

def euclidean(v1, v2):
    """Euclidean distance score"""
    return 1 / (1 + ((v1 - v2) ** 2).sum())

def pearsonr(v1, v2):
    """
    Pearson correlation
    :param v1, v2(1D numpy.ndarray)
    :rtype double
    """
    assert v1.shape == v2.shape
    n = v1.size
    s1 = v1.sum()
    s2 = v2.sum()
    s1_sq = (v1 ** 2).sum()
    s2_sq = (v2 ** 2).sum()
    psum = (v1 * v2).sum()
    num = psum - s1 * s2 / n
    den = np.sqrt((s1_sq - s1 ** 2 / n) * (s2_sq - s2 ** 2 / n))
    return num / den if den else .0  # den == 0
